package cm.just;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author wujiaming
 */
@WebServlet(urlPatterns = "/bmi")
public class BmiServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String height = req.getParameter("height");
        String weight = req.getParameter("weight");
        double h = Double.parseDouble(height);
        double w = Double.parseDouble(weight);

        //计算体脂率
        double bmi = w/(h*h);
        System.out.println(bmi);

        String info = null;
        if(bmi<18.5){
            info = "偏瘦";
        }else if(bmi>=18.5 && bmi<24){
            info= "标准体重";
        }else if(bmi>24 && bmi<28){
            info = "超重";
        }else{
            info = "肥胖";
        }
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter = resp.getWriter();
        printWriter.print("<h2>"+info+"</h2>");
    }
}
